<!DOCTYPE html><html lang="en"><head><title>payments/servlet/GetPaymentServlet</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content="../../"><meta name="groc-document-path" content="payments/servlet/GetPaymentServlet"><meta name="groc-project-path" content="src/main/java/com/paypal/api/payments/servlet/GetPaymentServlet.java"><link rel="stylesheet" type="text/css" media="all" href="../../assets/style.css"><script type="text/javascript" src="../../assets/behavior.js"></script><body><div id="meta"><div class="file-path">src/main/java/com/paypal/api/payments/servlet/GetPaymentServlet.java</div></div><div id="document"><div class="segment"><div class="comments "><div class="wrapper"><h1 id="getpayment-sample">GetPayment Sample</h1>
<p>This sample code demonstrates how you can retrieve
the details of a payment resource.
API used: /v1/payments/payment/{payment-id}</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">package</span> com.paypal.api.payments.servlet;

<span class="hljs-keyword">import</span> java.io.IOException;
<span class="hljs-keyword">import</span> java.io.InputStream;

<span class="hljs-keyword">import</span> javax.servlet.ServletConfig;
<span class="hljs-keyword">import</span> javax.servlet.ServletException;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServlet;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;

<span class="hljs-keyword">import</span> org.apache.log4j.Logger;

<span class="hljs-keyword">import</span> com.paypal.api.payments.Payment;
<span class="hljs-keyword">import</span> com.paypal.api.payments.util.GenerateAccessToken;
<span class="hljs-keyword">import</span> com.paypal.api.payments.util.ResultPrinter;
<span class="hljs-keyword">import</span> com.paypal.base.rest.PayPalRESTException;
<span class="hljs-keyword">import</span> com.paypal.base.rest.PayPalResource;</div></div></div><div class="segment"><div class="comments doc-section"><div class="wrapper"><p><span class='doc-section-header'> author lvairamani</span></p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GetPaymentServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">HttpServlet</span> </span>{

	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">1L</span>;
	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Logger LOGGER = Logger
			.getLogger(GetPaymentServlet.class);

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">(ServletConfig servletConfig)</span> <span class="hljs-keyword">throws</span> ServletException </span>{</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h2 id="load-configuration">Load Configuration</h2>
<p>Load SDK configuration for
the resource. This intialization code can be
done as Init Servlet.</p></div></div><div class="code"><div class="wrapper">		InputStream is = GetPaymentServlet.class
				.getResourceAsStream(<span class="hljs-string">"/sdk_config.properties"</span>);

		<span class="hljs-keyword">try</span> {
			PayPalResource.initConfig(is);
		} <span class="hljs-keyword">catch</span> (PayPalRESTException e) {
			LOGGER.fatal(e.getMessage());
		}

	}

	<span class="hljs-annotation">@Override</span>
	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doGet</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>{
		doPost(req, resp);

	}</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h2 id="getpayment">GetPayment</h2>
<p>Call the method with a valid Payment ID</p></div></div><div class="code"><div class="wrapper">	<span class="hljs-annotation">@Override</span>
	<span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doPost</span><span class="hljs-params">(HttpServletRequest req, HttpServletResponse resp)</span>
			<span class="hljs-keyword">throws</span> ServletException, IOException </span>{
		<span class="hljs-keyword">try</span> {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="accesstoken">AccessToken</h3>
<p>Retrieve the access token from
OAuthTokenCredential by passing in
ClientID and ClientSecret
It is not mandatory to generate Access Token on a per call basis.
Typically the access token can be generated once and
reused within the expiry window</p></div></div><div class="code"><div class="wrapper">			String accessToken = GenerateAccessToken.getAccessToken();</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Retrieve the payment object by calling the
static <code>get</code> method
on the Payment class by passing a valid
AccessToken and Payment ID</p></div></div><div class="code"><div class="wrapper">			Payment payment = Payment.get(accessToken,
					<span class="hljs-string">"PAY-0XL713371A312273YKE2GCNI"</span>);
			LOGGER.info(<span class="hljs-string">"Payment retrieved ID = "</span> + payment.getId()
					+ <span class="hljs-string">", status = "</span> + payment.getState());
			ResultPrinter.addResult(req, resp, <span class="hljs-string">"Get Payment"</span>, Payment.getLastRequest(), Payment.getLastResponse(), <span class="hljs-keyword">null</span>);
		} <span class="hljs-keyword">catch</span> (PayPalRESTException e) {
			ResultPrinter.addResult(req, resp, <span class="hljs-string">"Get Payment"</span>, Payment.getLastRequest(), <span class="hljs-keyword">null</span>, e.getMessage());
		}
		req.getRequestDispatcher(<span class="hljs-string">"response.jsp"</span>).forward(req, resp);
	}

}</div></div></div></div></body></html>